/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * FormatMillsTag.java
 * wholebasic
 * 2014年3月14日上午10:23:30
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.web.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.joda.time.DateTime;

public class FormatMillisTag extends SimpleTagSupport {
    private long value;
    private String pattern = "Y-M-d H:m:s";
	private boolean millis=false;   
    @Override
	public void doTag() throws JspException, IOException {
    	JspContext jctx = getJspContext();
    	JspWriter jwriter = jctx.getOut(); 	
    
    	if(isMillis()){
    		 jwriter.write(String.valueOf(DateTime.now().getMillis()));
    		 return;
    	}
    	
    	DateTime dt = (value==0)?DateTime.now():DateTime.now().withMillis(value);
   	
    	if(getPattern()==null){
	    	 jwriter.write(dt.toString());
	    }else{
	    	 jwriter.write(dt.toString(getPattern())); 
	    }
    	
	}

	public long getValue() {
		return value;
	}

	public void setValue(long value) {
		this.value = value;
	}

	public boolean isMillis() {
		return millis;
	}

	public void setMillis(boolean millis) {
		this.millis = millis;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
       
}
